﻿using BaseAuthRole.Domain.Input;
using BaseAuthRole.IService;
using Lotus.AspNetCore;
using Lotus.Domain.Input;
using Lotus.Domain.Input.Lotus;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RS.BaseModel.Common;

namespace BaseAuthRole.Web.Api.Init
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InitController : ControllerBase
    {

        private IUserService _userService;

        private IFreeSql  _freeSql;

        private IBaseLotusClient _baseLotusClient;
        public InitController(IUserService userService, IFreeSql freeSql, IBaseLotusClient baseLotusClient)
        {
            this._userService = userService;
            this._freeSql = freeSql;
            this._baseLotusClient = baseLotusClient;
        }

        /// <summary>
        /// 路特斯用户信息初始化
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult LtsUserInfoInit([FromBody]GetLotusUserInfoListInput input)
        {

            var result = new CommonResult();

            var info = this._baseLotusClient.GetLotusUserInfoList(input);

            if (!info.IsError) {

                if (info.Result.Data.DataList.Count > 0)
                {
                    var temp = info.Result.Data.DataList.Where(p => p.EmployeeTypeCode != "7" && p.EmployeeTypeCode != "98" && p.State != null && p.Gender != null).ToList().Select(o => new AddOrUpdateUserInput
                    {
                        Id=o.UserId,
                        Name=o.Name,
                        LoginName=o.UserName,
                        Picture=o.Picture,
                        Phone=o.Mobile,
                        JobNumber=o.EmployeeNo,
                        NickName=o.NickName,
                        Department=o.DeptName,
                        DepartmentId=o.DeptId,
                        JobTitle=o.JobTitle,
                        JobState=o.State.Value,
                        Email=o.Email,
                        IdCardNo=o.IdCardNo,
                        NationalityCode=o.NationalityCode,
                        Nationality=o.Nationality,
                        Gender=o.Gender.Value,
                        TopQualifications=o.TopQualifications,
                        Nation=o.Nation,
                        NationPlace=o.NationPlace,
                        EmployeeType=o.EmployeeTypeCode,
                        CompanyAge=o.CompanyAge,
                        PositionName=o.PositionName,
                        JobLevel=o.JobLevel,
                        StaffSystem=o.StaffSystem,
                        StaffSystemCode=o.StaffSystemCode,
                        BirthDate=o.BirthDate,
                        FSUnionId=o.FSUnionId,
                        Company=o.Company,
                    }).ToList();
                    result=_userService.AddOrUpdateUser(temp);
                    return Ok(result);
                }            
            }
            return Ok(result);
        }



        [HttpPost]
        public IActionResult LtsOrgInfoInit([FromBody] GetLotusOrgInfoListInput input) {

            var result = new CommonResult();
            var info = this._baseLotusClient.GetLotusOrgInfoList(input);
            if (!info.IsError)
            {
                if (info.Result.Data.DataList.Count > 0)
                {
                    var temp = info.Result.Data.DataList.Select(o => new AddOrUpdateOrgInput
                    {
                        Id=o.OrgId,
                        OrgName = o.OrgName,
                        EnName = o.EnName,
                        FullName = o.FullName,
                        ParentId = o.ParentId,
                        Type = o.Type,
                        State = o.State,
                        Sapcode = o.Sapcode,
                        CostCenter = o.CostCenter,
                        CostCenterCode = o.CostCenterCode,
                        WechatId = o.WechatId,
                        FSOrgId = o.FSOrgId,
                        FSOrgChatId = o.FSOrgChatId,
                        OrgManger = o.OrgManger,
                        OrgMangeraCode = o.OrgMangeraCode,
                        EffectiveTime = o.EffectiveTime,
                        ExpirationTime = o.ExpirationTime,
                        Level = o.Level,
                        Property = o.Property,
                    }).ToList();
                    result = _userService.AddOrUpdateOrg(temp);
                    return Ok(result);
                }
            }
            return Ok(result);

        }

        [HttpGet]
        public IActionResult LtsSupplyInit(int maxPageSize)
        {
            for (int i = 1; i <= maxPageSize; i++)
            {
                GetLotusUserInfoListInput input = new GetLotusUserInfoListInput()
                {
                    StartTime = null,
                    PageNo = i,
                    PageSize = 200,
                    OrgCode = null,
                    Name = null,
                    Mobile = null,
                    EmpNos = null,
                    StaffSystemCodeList = new List<string>(),
                    UserName = null,
                    Status = null
                };

                var info = this._baseLotusClient.GetLotusUserInfoList(input);

                if (!info.IsError)
                {
                    if (info.Result.Data.DataList.Count > 0)
                    {
                        var temp = info.Result.Data.DataList.Where(p => p.EmployeeTypeCode == "99" && p.State != null && p.Gender != null).Select(o => new AddOrUpdateUserInput
                        {
                            Id=o.UserId,
                            Name=o.Name,
                            LoginName=o.UserName,
                            Picture=o.Picture,
                            Phone=o.Mobile,
                            JobNumber=o.EmployeeNo,
                            NickName=o.NickName,
                            Department=o.DeptName,
                            DepartmentId=o.DeptId,
                            JobTitle=o.JobTitle,
                            JobState=o.State.Value,
                            Email=o.Email,
                            Company =o.Company,
                            IdCardNo=o.IdCardNo,
                            NationalityCode=o.NationalityCode,
                            Nationality=o.Nationality,
                            Gender=o.Gender.Value,
                            TopQualifications=o.TopQualifications,
                            Nation=o.Nation,
                            NationPlace=o.NationPlace,
                            EmployeeType=o.EmployeeTypeCode,
                            CompanyAge=o.CompanyAge,
                            PositionName=o.PositionName,
                            JobLevel=o.JobLevel,
                            StaffSystem=o.StaffSystem,
                            StaffSystemCode=o.StaffSystemCode,
                            BirthDate=o.BirthDate,
                            FSUnionId=o.FSUnionId,
                        }).ToList();

                        if (temp.Count > 0)
                        {
                            try
                            {
                                var result = _userService.AddOrUpdateUser(temp);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("ex:" + ex.ToString());
                            }
                        }
                    }
                }
            }
            return Ok("完成");
        }
    }
}
